var dbConfig = require("../config/dbconfig");
const fs = require("fs");
// 导入语言入库
async function getFileInfo() {
  const FileMap = fs.readFileSync("./public/locales/en.json", "utf8");
  const fileList = [];
  const parseFileMap = JSON.parse(FileMap);
  for (const serial in parseFileMap) {
    fileList.push({
      serial,
      value: parseFileMap[serial],
    });
  }
  return fileList;
}
async function checkExistSerial(fileItem) {
  let sql = `select * from language where serial=?`;
  let sqlArr = [fileItem.serial];
  let result = await dbConfig.SySqlConnect(sql, sqlArr);
  return result;
}
async function leadingZhTable() {
  const fileInfo = await getFileInfo();
  if (!fileInfo) return;
  for (const fileItem of fileInfo) {
    const checkResult = await checkExistSerial(fileItem);
    // 已经入库不在保存
    if (checkResult.length) return;
    let sql = `insert into language (serial,zh) values (?,?)`;
    let sqlArr = [fileItem.serial, fileItem.value];
    let result = await dbConfig.SySqlConnect(sql, sqlArr);
    console.log(result, 222);
  }
}
async function leadingEnTable() {
  const fileInfo = await getFileInfo();
  if (!fileInfo) return;
  for (const fileItem of fileInfo) {
    let sql = `update language set en=? where serial=?`;
    let sqlArr = [fileItem.value, fileItem.serial];
    let result = await dbConfig.SySqlConnect(sql, sqlArr);
  }
}
module.exports = {
  leadingZhTable,
  leadingEnTable,
};
